package com.example.book_libapp;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Booksearch extends Activity {

	private EditText edsearchkey;
	private Button btnsearch;
	private RecordsDbHelper mDbHelper;
	private CheckBox checkcategory;
	private CheckBox checksort;
	private Spinner spinnercat;
	private Spinner spinnersort;
	static final String TAG_KEY = "query";
	static final String TAG_SORT = "sort";
	static final String TAG_SORTT = "sort_type";
	static final String TAG_CAT = "category";
	

	public void onSearch(View v) {
		onSearchRequested();
	}

	public void onHome(View v) {
		return2Home(this);
	}

	public void return2Home(Context context) {
		final Intent intent = new Intent(context, MainActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(intent);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_search);
		checkcategory = (CheckBox) findViewById(R.id.checkcategory);
		checksort = (CheckBox) findViewById(R.id.checksort);
		spinnercat = (Spinner) findViewById(R.id.spincategory);
		spinnersort = (Spinner) findViewById(R.id.spinsort);
		
//		btnsearch = (Button)findViewById(R.id.BtnSearch);
//		
//		btnsearch.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				
//				
//			}
//		});
		
		// Adapter for spinner
        ArrayAdapter<?> adapter1 = ArrayAdapter.createFromResource(getApplicationContext(),  R.array.spinnersort, android.R.layout.simple_spinner_dropdown_item);
        // Sets the layout resource to create the drop down views
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 
        //The Adapter is used to provide the data which backs this Spinner.
        spinnersort.setAdapter(adapter1);
		
		// Adapter for spinner
        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(getApplicationContext(),  R.array.spinnercategory, android.R.layout.simple_spinner_dropdown_item);
        // Sets the layout resource to create the drop down views
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 
        //The Adapter is used to provide the data which backs this Spinner.
        spinnercat.setAdapter(adapter);
		
        
		checkcategory.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (checkcategory.isChecked()) {
					spinnercat.setEnabled(true);
				} else {
					spinnercat.setEnabled(false);
				}

			}
		});
		checksort.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (checksort.isChecked()) {
					spinnersort.setEnabled(true);
				} else {
					spinnersort.setEnabled(false);
				}
			}
		});

		Intent intent = getIntent();

		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			Log.e("search", "3333333333333333333333333332");
			String query = intent.getStringExtra(SearchManager.QUERY);

			showResults(query);
		} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {

			Intent recordIntent = new Intent(this, RecordActivity.class);
			recordIntent.setData(intent.getData());
			startActivity(recordIntent);
			finish();
		}

		btnsearch = (Button) findViewById(R.id.BtnSearch);
		edsearchkey = (EditText) findViewById(R.id.EditName);
		btnsearch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				
				String search_key = ((TextView) findViewById(R.id.EditName))
						.getText().toString();
				Intent i = new Intent(getApplicationContext(),
						Booksearchresults.class);
				i.putExtra(TAG_KEY, search_key);
				i.putExtra(TAG_SORT, search_key);
				i.putExtra(TAG_SORTT, search_key);
//				i.putExtra(TAG_KEY, search_key);
				startActivity(i);
			}
		});

	}
	
	 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     	
         // On selecting a spinner item
         String item = parent.getItemAtPosition(position).toString();
         
         // showing a toast on selecting an item 
         Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();
  
     }
  
     public void onNothingSelected(AdapterView<?> arg0) {
  
     }
	

	private void showResults(String query) {

		Cursor cursor = managedQuery(SuggestionProvider.CONTENT_URI, null,
				null, new String[] { query }, null);

		if (cursor == null) {
			Toast.makeText(this, "There are no results ", Toast.LENGTH_SHORT)
					.show();
			Log.e("search", "" + query);
		} else {
			Log.e("search", "" + query);
			String[] from = new String[] { RecordsDbHelper.KEY_DATA };

			// int[] to = new int[] { R.id.text1 };
			// SimpleCursorAdapter records = new SimpleCursorAdapter(this,
			// R.layout.record, cursor, from, to);
			// getListView().setAdapter(records);
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.search_record:
			onSearchRequested();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}